---
--类名: CountDownView.lua
--创建日期: 2017-5-19
--创建人: 陈小虎
--功能描述: 
--修改内容:
--修改日期:   
--修改人:     
--修改备注: 
--

local TimeUtil_secondsToTime = TimeUtil.secondsToTime

local CountDownView = class("CountDownView",function()
    return display.newLayer("CountDownView")
end)

function CountDownView:ctor(params)
	self._startSecond = app:getTime()
	local ttfConfig = {}
    ttfConfig.fontFilePath = res.font
    ttfConfig.fontSize = params.fontSize
    self._word = cc.Label:createWithTTF(ttfConfig,"", cc.TEXT_ALIGNMENT_LEFT)
    self._word:setTextColor(cc.c4b(params.r, params.g, params.b, params.o))
    self._word:setPosition(cc.p(params.x,params.y))
    self:addChild(self._word)

    local hour = params.hour or 0
    local min = params.min or 0
    local sec = params.sec or 0

    local totalSec = hour * 60 * 60 + min * 60 + sec

    self:runAction(cc.RepeatForever:create(cc.Sequence:create(cc.CallFunc:create(function()
    	if self._word then
    		local movetime = app:getTime() - self._startSecond
    		local countdownTime = totalSec - movetime
    		if countdownTime <= 0 then
    			countdownTime = 0
    		end

            local timeStr = TimeUtil_secondsToTime(countdownTime)
            self._word:setString(timeStr)

	    	if countdownTime == 0 then
	    		self:callOver()
	    		UIHandler:removeCountDown()
	    	end
    	end
    end),cc.DelayTime:create(1))))

    self:setOverCall(params.callback)
end

function CountDownView:setOverCall(callback)
	self._overCall = callback
end

function CountDownView:callOver()
	if self._overCall then
		self._overCall()
	end
end

return CountDownView